﻿using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using HT.Framework;
using UnityEngine;

namespace TG
{
    public static class GameSetting
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        #region 设置配置数据

        /// <summary>
        /// 0.横屏  1.竖屏  2球面
        /// </summary>
        public static int configSceneModel = 2;

        /// <summary>
        /// 1.高空 0.低空
        /// </summary>
        public static int configSceneModelType = 1;

        public static int configOffset = 1920;
        public static int configWidth = 1920;
        public static int configHeight = 2400;
        public static int configFullScreen = 0;
        public static float ignoreX = 0;
        public static float ignoreY = 0;


        #endregion
        
        private const uint SWP_SHOWWINDOW = 0x0040;

        public static void ReadConfig()
        {
            string path = FileWriter.GetFullPath("config");
            if (File.Exists(path))
            {
                string[] lines = File.ReadAllLines(path);
                foreach (var str in lines)
                {
                    if (str.StartsWith("sceneModel"))
                    {
                        string[] splits = str.Split('=');
                        configSceneModel = int.Parse(splits[1]);
                    }
                    else if (str.StartsWith("offset"))
                    {
                        string[] splits = str.Split('=');
                        configOffset = int.Parse(splits[1]);
                    }
                    else if (str.StartsWith("with"))
                    {
                        string[] splits = str.Split('=');
                        configWidth = int.Parse(splits[1]);
                    }
                    else if (str.StartsWith("height"))
                    {
                        string[] splits = str.Split('=');
                        configHeight = int.Parse(splits[1]);
                    }
                    else if (str.StartsWith("fullScreen"))
                    {
                        string[] splits = str.Split('=');
                        configFullScreen = int.Parse(splits[1]);
                    }
                    else if (str.StartsWith("ignoreX"))
                    {
                        string[] splits = str.Split('=');
                        ignoreX = float.Parse(splits[1]);
                    }
                    else if (str.StartsWith("ignoreY"))
                    {
                        string[] splits = str.Split('=');
                        ignoreY = float.Parse(splits[1]);
                    }
                }
            }

            Debug.Log(
                $"读取配置 sceneModel {configSceneModel} offset {configOffset} with {configWidth} height {configHeight} fullScreen {configFullScreen} ignoreX {ignoreX} ignoreY {ignoreY}");
        }

        /// <summary>
        /// 分辨率设置
        /// </summary>
        public static void SetScreenDisplay()
        {
            QualitySettings.SetQualityLevel(QualitySettings.names.Length - 1);

            int width = configWidth, height = configHeight;
            Screen.SetResolution(width, height, configFullScreen == 1); //设置主屏的分辨率  可以进行动态的更新分辨率

            Debug.Log(
                $"屏幕模式：{configFullScreen} Screen：{Screen.fullScreenMode} 屏幕偏移距离 {configOffset} 配置值 w : {width} h : {height} 分辨率 {Screen.width} * {Screen.height}");

            Main.m_Coroutiner.StartCoroutine(SetPostion(configOffset, 0, width, height));
        }

        static IEnumerator SetPostion(int posx, int posy, int width, int height)
        {
            yield return new WaitForEndOfFrame();
            SetWindowPos(GetForegroundWindow(), 0, posx, posy, width, height, SWP_SHOWWINDOW);
            //再次设置分辨率
            Screen.SetResolution(width, height, configFullScreen == 1); //设置主屏的分辨率  可以进行动态的更新分辨率

            Debug.Log(
                $"再次设置分辨率 屏幕模式：{configFullScreen} Screen：{Screen.fullScreenMode} 屏幕偏移距离 {configOffset} 配置值 w : {width} h : {height} 分辨率 {Screen.width} * {Screen.height}");
        }
    }
}